package cn.iocoder.yudao.module.common.controller.admin.textbookversion;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.common.controller.admin.textbookversion.vo.*;
import cn.iocoder.yudao.module.common.dal.dataobject.textbookversion.TextbookVersionDO;
import cn.iocoder.yudao.module.common.service.textbookversion.TextbookVersionService;

@Tag(name = "管理后台 - 教材版本")
@RestController
@RequestMapping("/common/textbook-version")
@Validated
public class TextbookVersionController {

    @Resource
    private TextbookVersionService textbookVersionService;

    @PostMapping("/create")
    @Operation(summary = "创建教材版本")
    @PreAuthorize("@ss.hasPermission('common:textbook-version:create')")
    public CommonResult<Long> createTextbookVersion(@Valid @RequestBody TextbookVersionSaveReqVO createReqVO) {
        return success(textbookVersionService.createTextbookVersion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新教材版本")
    @PreAuthorize("@ss.hasPermission('common:textbook-version:update')")
    public CommonResult<Boolean> updateTextbookVersion(@Valid @RequestBody TextbookVersionSaveReqVO updateReqVO) {
        textbookVersionService.updateTextbookVersion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除教材版本")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('common:textbook-version:delete')")
    public CommonResult<Boolean> deleteTextbookVersion(@RequestParam("id") Long id) {
        textbookVersionService.deleteTextbookVersion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得教材版本")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('common:textbook-version:query')")
    public CommonResult<TextbookVersionRespVO> getTextbookVersion(@RequestParam("id") Long id) {
        TextbookVersionDO textbookVersion = textbookVersionService.getTextbookVersion(id);
        return success(BeanUtils.toBean(textbookVersion, TextbookVersionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得教材版本分页")
    @PreAuthorize("@ss.hasPermission('common:textbook-version:query')")
    public CommonResult<PageResult<TextbookVersionRespVO>> getTextbookVersionPage(@Valid TextbookVersionPageReqVO pageReqVO) {
        PageResult<TextbookVersionDO> pageResult = textbookVersionService.getTextbookVersionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TextbookVersionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出教材版本 Excel")
    @PreAuthorize("@ss.hasPermission('common:textbook-version:export')")
    @OperateLog(type = EXPORT)
    public void exportTextbookVersionExcel(@Valid TextbookVersionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TextbookVersionDO> list = textbookVersionService.getTextbookVersionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "教材版本.xls", "数据", TextbookVersionRespVO.class,
                        BeanUtils.toBean(list, TextbookVersionRespVO.class));
    }

}